﻿using App.Model;
using App.Untiys;
using DtRlDeviceApiNet.Models;
using SqlSugar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Win.Dal;

namespace App.Desk.Pro.SamplingData
{
    public partial class FrmAdd : UIEditForm
    {
        public FrmAdd()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (!UIMessageBox.ShowAsk($"是否添加{txtPlanCount.Value}节计划？"))
                    return;
                //先用数据库中的数据随机
                var trainInfos = ProSamplingServices.GetRandomList(txtPlanCount.Value);
                if (!trainInfos.IsNotNull())//如果数据库中没有数据则 全新生成
                    trainInfos = GetRandomSampling(txtPlanCount.Value);
                else if (trainInfos.Count < txtPlanCount.Value)
                    trainInfos = GetRandomSampling(txtPlanCount.Value);
                var newLocalBatch = ProSamplingServices.GetNewLocalBatch();
                if (newLocalBatch <= 0)
                {
                    UIMessageBox.ShowError("生成本地批次异常");
                    return;
                }
                List<ProSampling> samplings = new List<ProSampling>();
                int TrainIndex = 1;
                if (trainInfos != null)
                    if (trainInfos.Count > 0)
                    {
                        trainInfos.ForEach(res =>
                        {
                            samplings.Add(new ProSampling
                            {
                                LocalBatch = newLocalBatch,
                                ActualSimplingLocation = "",//实际采样坐标，
                                SamplingPerson = Config.CurrentUser.ChName,
                                BatchInfoId = new Random(DateTime.Now.Millisecond + TrainIndex).Next(1000, 2000).ToString(),
                                CanSampling = res.CanSampling,
                                CarNo = new Random(DateTime.Now.Millisecond + TrainIndex).Next(1000000, 2000000).ToString(),
                                CarType = res.CarType,
                                CreateTime = DateTime.Now,
                                IsSuccess = "待采",
                                IsUpload = "测试",
                                IsWeight = 0,
                                OrgId = new Random(DateTime.Now.Millisecond + TrainIndex).Next(3000, 4000).ToString(),
                                SamplingEndTime = null,
                                SamplingId = 0,
                                SamplingStartTime = null,
                                SdxId = new Random(DateTime.Now.Millisecond + TrainIndex).Next(5000, 6000).ToString(),
                                SetBucketNo = "",
                                SimplingCode = res.SimplingCode,
                                SimplingLocation = res.SimplingLocation,
                                SimplingPic = "",
                                SimplingPoint = 2,
                                SimplingSource = res.SimplingSource,
                                SimplingWeight = "",
                                Stretchposition = res.Stretchposition,
                                SupplierCode = "KCP7CW1",//res.SupplierCode,
                                SysId = new Random(DateTime.Now.Millisecond + TrainIndex).Next(7000, 8000).ToString(),
                                TrainIndex = TrainIndex,
                                TrainSamplingCode = res.TrainSamplingCode,
                                TransLong = res.TransLong,
                                TransWide = res.TransWide,
                                TravelNo = DateTime.Now.ToString("yyyyMMdd") + TrainIndex.ToString("00"),
                                VirtuallySimplingLocation = "",
                                IsTest = "是"
                            });
                            TrainIndex++;
                        });
                        if (ProSamplingServices.Add(samplings))
                        {
                            UIMessageBox.ShowSuccess("添加成功");
                            this.Close();
                        }
                    }
            }
            catch (Exception ex)
            {
                UIMessageBox.ShowSuccess("添加失败" + ex.Message);
                LogHelper.DoWriteLogType(LogType.Plan, $"添加失败{ex.Message}");
            }
        }

        public List<ProSampling> GetRandomSampling(int count)
        {
            List<string> posList = new List<string>();
            for (int i = 1; i <= 100; i++)
                posList.Add($"{258 + new Random(DateTime.Now.Millisecond + i + 1).Next(0, 30)},{172 + new Random(DateTime.Now.Millisecond + i + 2).Next(0, 30)},0|{917 - +new Random(DateTime.Now.Millisecond + i + 3).Next(0, 30)},{273 - +new Random(DateTime.Now.Millisecond + i + 4).Next(0, 30)},1");
            List<ProSampling> samplings = new List<ProSampling>();
            for (int i = 1; i <= count; i++)
                samplings.Add(new ProSampling
                {
                    LocalBatch = 0,
                    ActualSimplingLocation = "",//实际采样坐标，
                    SamplingPerson = Config.CurrentUser.ChName,
                    BatchInfoId = "ae440784-62d6-4272-bb22-" + new Random(DateTime.Now.Millisecond + i).Next(1000, 2000).ToString(),
                    CanSampling = 1,
                    CarNo = new Random(DateTime.Now.Millisecond + i).Next(1000, 2000).ToString(),
                    CarType = "C64K",
                    CreateTime = DateTime.Now,
                    IsSuccess = "待采",
                    IsUpload = "待传",
                    IsWeight = 0,
                    OrgId = new Random(DateTime.Now.Millisecond + i).Next(3000, 4000).ToString(),
                    SamplingEndTime = null,
                    SamplingId = 0,
                    SamplingStartTime = null,
                    SdxId = new Random(DateTime.Now.Millisecond + i).Next(5000, 6000).ToString(),
                    SetBucketNo = "",
                    SimplingCode = "HC02",
                    SimplingLocation = posList[new Random(DateTime.Now.Millisecond + i).Next(0, posList.Count - 1)],
                    SimplingPic = "",
                    SimplingPoint = 2,
                    SimplingSource = "",
                    SimplingWeight = "",
                    Stretchposition = "",
                    SupplierCode = "KCP7CW1",
                    SysId = new Random(DateTime.Now.Millisecond + i).Next(7000, 8000).ToString(),
                    TrainIndex = i,
                    TrainSamplingCode = "",
                    TransLong = 0,
                    TransWide = 0,
                    TravelNo = DateTime.Now.ToString("yyyyMMdd") + i.ToString("00"),
                    VirtuallySimplingLocation = "",
                    IsTest = "是"
                });
            return samplings;
        }
    }
}
